package com.fingard.dsp.bank.directbank.cmb01;

import java.io.File;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillTrans;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.*;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.detail.*;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
* @Author Green
* */
public class CMB01ElecBillTrans extends CMB01Base implements IElecBillTrans {
    @Override
    /**
     * 通用签收
     */
    public RetAtsB003BillRespSubmit D_B003_BillResp_Submit(ReqB003BillRespSubmit query) throws Exception {
        RetAtsB003BillRespSubmit retAts = new RetAtsB003BillRespSubmit(query.reqHeader);
        RetAtsB003BillRespSubmitDetail detail  = new RetAtsB003BillRespSubmitDetail();
        retAts.writeStart();
        CMB01Header header =  new CMB01Header();
        header.FUNNAM = "SDKNTOPRANS";
        XmlTextWriter xmlWriter =  new XmlTextWriter();
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRANSX");
        Date d=  new Date();
        String date  = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        //期望日
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        //期望日期
        xmlWriter.writeElementString("EPTTIM",time);
        //申请人行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.actArea);
        //当开户行是总行时，申请人行号传1
        if("308584001024".equals(query.detail.respBnkCode)){
            bankarea = "1";
        }
        //如果ats传过来是直联区域，则直接发送给银行(直联区域2位)
        xmlWriter.writeElementString("APPBBK",4==query.detail.actArea.length()?bankarea:query.detail.actArea);
        //申请人账号
        xmlWriter.writeElementString("APPEAC",query.detail.actNum);
        //业务类型  03=提示收票回复
        //02=提示承兑回复
        //25=央行卖票回复
        //23=同意清偿回复21=逾期提示付款回复
        //19=质押解除回复
        //20=提示付款回复
        //18=质押申请回复
        //17=保证回复
        //12=贴现赎回回复
        //11=贴现回复
        //10=背书转让回复
        String busCode;
//        1-承兑应答 2-收票应答 3-背书转让应答 4-回购式贴现赎回应答 5-质押解除应答 6-提示付款应答 7-保证应答 8-同意清偿应
        switch (Integer.valueOf(query.detail.respType)){
            case 1:busCode ="02";break;
            case 2:busCode ="03";break;
            case 3:busCode ="10";break;
            case 4:busCode ="12";break;
            case 5:busCode ="19";break;
            case 6:if("1".equals(query.detail.proPayType)){
                busCode = "20";
            }else{
                busCode = "21";
            }break;
            case 7:busCode ="17";break;
            case 8:busCode ="23";break;
            case 9:busCode ="18";break;
            default:throw new Exception("未知的业务类型："+query.detail.respType);
        }
        xmlWriter.writeElementString("BUSCOD",busCode);
        //回复标记 SU01=拒绝签收
        //SU00=同意签收
        if("1".equals(query.detail.respFlag)) {
            xmlWriter.writeElementString("RPYCOD", "SU00");
        }else{
            xmlWriter.writeElementString("RPYCOD","SU01");
        }
        //到期无条件支付委托
        if("1".equals(query.detail.respType)) {
//            CC01=不含委托/不承诺兑付  CC00=含委托/承诺兑付.
            if("1".equals(query.detail.respFlag)) {
                // TODO: 2019/8/13 同意签收能否传CC01需要等银行回复
                if("1".equals(query.detail.mustPay)){
                    xmlWriter.writeElementString("PAYAGT", "CC00");
                }else{
                    throw new Exception("承兑同意签收时，到期无条件支付委托只能传【CC00】");
                }
            }else{
                xmlWriter.writeElementString("PAYAGT", "CC00");
            }
            xmlWriter.writeElementString("CNVNBR",query.detail.protocolNo);
        }else{
            xmlWriter.writeElementString("PAYAGT", "");
            xmlWriter.writeElementString("CNVNBR","");
        }

        if("6".equals(query.detail.respType)&&"2".equals(query.detail.respFlag)) {
            if(StringHelper.isNullOrEmpty(query.detail.unPayCode)){
                throw new Exception("提示付款拒签时，拒付理由不能为空!");
            }
            if("99".equals(query.detail.unPayCode)){
                xmlWriter.writeElementString("RFSCOD", "DC09");
                xmlWriter.writeElementString("DESTXT",query.detail.unPayMemo);
            }else{
                xmlWriter.writeElementString("RFSCOD", "DC0" + (Integer.valueOf(query.detail.unPayCode) - 1));
                xmlWriter.writeElementString("DESTXT", "");
            }
        }else{
            xmlWriter.writeElementString("RFSCOD", "");
            xmlWriter.writeElementString("DESTXT","");
        }
        if("1".equals(query.detail.respType)) {
            xmlWriter.writeElementString("ACPRAT", query.detail.accpCredit);
            xmlWriter.writeElementString("ACPCAG", query.detail.accpGrade);
            xmlWriter.writeElementString("ACPDUE", DateHelper.convertD10toD8(query.detail.accpGradeDue));
        }else{
            xmlWriter.writeElementString("ACPRAT", "");
            xmlWriter.writeElementString("ACPCAG", "");
            xmlWriter.writeElementString("ACPDUE", "");
        }
        xmlWriter.writeElementString("OPCREM",query.detail.memo);
        xmlWriter.writeEndElement();
        xmlWriter.writeStartElement("NTOPRANSX1");
        String [] clts = query.detail.busiNo.split("@");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        //服务平台键值,从另一个接口获取
        xmlWriter.writeElementString("CLTKEY",clts[0]);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");
                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

//    private void saveAppleDate(String reqSeqID, String applyDate)throws Exception {
//        String filepathDir = getBankConfig().getValueAsString("ApplyDateSave");
//        String filename = filepathDir+ File.separator+applyDate+"_"+reqSeqID+".txt";
//        FileHelper.writeFile(filename,applyDate+"_"+reqSeqID,getCharset());
//    }

    @Override
    /**
    * 出票
    * */
    public RetAtsB101BillApplySubmit D_B101_BillResp_Submit(ReqB101BillApplySubmit query) throws Exception {
        RetAtsB101BillApplySubmit retAts = new RetAtsB101BillApplySubmit(query.reqHeader);
        RetAtsB101BillApplySubmitDetail detail  = new RetAtsB101BillApplySubmitDetail();
        retAts.writeStart();
        CMB01Header header =  new CMB01Header();
        header.FUNNAM = "SDKNTOPRBRG";
        XmlTextWriter xmlWriter =  new XmlTextWriter();
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBRGX");
        Date d=  new Date();
        String date  = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        //期望日
        xmlWriter.writeElementString("EPTDAT",date);
        //期望日期
        xmlWriter.writeElementString("EPTTIM",time);
        //
        String billType;
        if("1".equals(query.detail.billType)){
            billType = "AC01";
        }else{
            billType = "AC02";
        }
        xmlWriter.writeElementString("BILTYP",billType);
        //票据币种
        xmlWriter.writeElementString("CCYNBR","10");
        //出票人客户类型
        xmlWriter.writeElementString("DRWCTP","RC01");
        //出票人分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.drawActArea);
        if("308584001024".equals(query.detail.drawBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("OPNBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("OPNBBK",4==query.detail.drawActArea.length()?bankarea:query.detail.drawActArea);

        //出票人帐号
        xmlWriter.writeElementString("OPNACC",query.detail.drawAct);
        xmlWriter.writeElementString("DRWRAT","");
        xmlWriter.writeElementString("DRWCAG","");
        xmlWriter.writeElementString("DRWDUE", "");
        //客户定制类型 01=出票、承兑、收票三合一
        //00=无定制
        if("1".equals(query.detail.autoPromptReceipt)) {
            xmlWriter.writeElementString("CLTCNV", "01");
        }else{
            xmlWriter.writeElementString("CLTCNV", "00");
        }
        xmlWriter.writeElementString("DESTXT",query.detail.memo);
        xmlWriter.writeEndElement();
        xmlWriter.writeStartElement("NTOPRBRGX1");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.reqHeader.reqSeqID);
        xmlWriter.writeElementString("BILAMT",query.detail.billAmt);
        //出票日期
        xmlWriter.writeElementString("ISUDAT",query.detail.issDate.substring(0,10).replaceAll("-",""));
        //到期日期
        xmlWriter.writeElementString("DUEDAT",query.detail.dueDate.substring(0,10).replaceAll("-",""));
        //不得转让标志
        if("1".equals(query.detail.transFlag)) {
            xmlWriter.writeElementString("TRFTAG", "EM00");
        }else{
            xmlWriter.writeElementString("TRFTAG", "EM01");
        }
        //收款人名称
        xmlWriter.writeElementString("OPCNAM",query.detail.payeeName);
        //收款人行号
        xmlWriter.writeElementString("OPCBRD",query.detail.payeeBnkCode);
        //收款人帐号
        xmlWriter.writeElementString("OPCEAC",query.detail.payeeAct);
        //交易合同编号
        xmlWriter.writeElementString("CNTNBR",query.detail.contractNo);
        //发票号码
        xmlWriter.writeElementString("INVNBR",query.detail.invoiceNo);
        //承兑人名称
        xmlWriter.writeElementString("ACPNAM",query.detail.accpName);
        //承兑人行号
        xmlWriter.writeElementString("ACPBRD",query.detail.accpBnkCode);
        //承兑人账号
        xmlWriter.writeElementString("ACPACC",query.detail.accpAct);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");
                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
    * 提示承兑
    * */
    public RetAtsB102BillAccpSubmit D_B102_BillResp_Submit(ReqB102BillAccpSubmit query) throws Exception {
        RetAtsB102BillAccpSubmit retAts = new RetAtsB102BillAccpSubmit(query.reqHeader);
        RetAtsB102BillAccpSubmitDetail detail  = new RetAtsB102BillAccpSubmitDetail();
        retAts.writeStart();
        CMB01Header header =  new CMB01Header();
        header.FUNNAM = "SDKNTOPRBRA";
        XmlTextWriter xmlWriter =  new XmlTextWriter();
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementClosed("MODALS");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBRAX");
        Date d=  new Date();
        String date  = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        //期望日
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        //期望日期
        xmlWriter.writeElementString("EPTTIM",time);
//        BCHNBR 报文子域批次号
        xmlWriter.writeElementString("BCHNBR","");
//        APPBBK 申请人分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.actArea);
        if("308584001024".equals(query.detail.drawBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.actArea.length()?bankarea:query.detail.actArea);

//        APPEAC 申请人账号
        xmlWriter.writeElementString("APPEAC",query.detail.actNum);
//        CNTNBR 交易合同编号
        xmlWriter.writeElementString("CNTNBR",query.detail.contractNo);
//        INVNBR 发票号码
        xmlWriter.writeElementString("INVNBR",query.detail.invoiceNo);
//        CLTREM 出票人备注
        xmlWriter.writeElementString("CLTREM",query.detail.memo);
        xmlWriter.writeEndElement();
        xmlWriter.writeStartElement("NTOPRECDX");

        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        //服务平台键值,从另一个接口获取
        xmlWriter.writeElementString("BILNBR ",query.detail.billNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
    * 撤票
    * */
    public RetAtsB103BillRetSubmit D_B103_BillResp_Submit(ReqB103BillRetSubmit query) throws Exception {
        RetAtsB103BillRetSubmit retAts =  new RetAtsB103BillRetSubmit(query.reqHeader);
        RetAtsB103BillRetSubmitDetail detail  = new RetAtsB103BillRetSubmitDetail();
        retAts.writeStart();
        query.readRecord();

        XmlTextWriter xmlWriter =  new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTOPRBRC";
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();
        xmlWriter.writeStartElement("NTOPRBRCX");
        Date d = new Date();
        String date  = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        //期望日
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        //期望日期
        xmlWriter.writeElementString("EPTTIM",time);
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.actArea);
        if("308584001024".equals(query.detail.actBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.actArea.length()?bankarea:query.detail.actArea);

        xmlWriter.writeElementString("APPEAC",query.detail.actNum);
        xmlWriter.writeElementString("CNLINF",StringHelper.hasAnyChar(query.detail.memo)?query.detail.memo:"无");
        xmlWriter.writeEndElement();
        xmlWriter.writeStartElement("NTOPRECDX");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        //服务平台键值,从另一个接口获取
        xmlWriter.writeElementString("BILNBR ",query.detail.billNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
    * 提示收票
    * */
    public RetAtsB104BillReceSubmit D_B104_BillResp_Submit(ReqB104BillReceSubmit query) throws Exception {
        RetAtsB104BillReceSubmit retAts = new RetAtsB104BillReceSubmit(query.reqHeader);
        RetAtsB104BillReceSubmitDetail detail =  new RetAtsB104BillReceSubmitDetail();

        retAts.writeStart();
        XmlTextWriter xmlWriter = new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTOPRBRP";
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBRPX");
        Date d=  new Date();
        String date = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        xmlWriter.writeElementString("EPTTIM",time);
        //申请人分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.actArea);
        if("308584001024".equals(query.detail.actBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.actArea.length()?bankarea:query.detail.actArea);

        //申请人帐号
        xmlWriter.writeElementString("APPEAC",query.detail.actNum);
        //出票人备注
        xmlWriter.writeElementString("CLTREM","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRECDX");
        //流水号
        xmlWriter.writeElementString("SQRNBR","0000000001");
        //客户参考号
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        //票号
        xmlWriter.writeElementString("BILNBR",query.detail.billNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
     * 背书转让申请
     * */
    public RetAtsB105BillEndoSubmit D_B105_BillResp_Submit(ReqB105BillEndoSubmit query) throws Exception {
        RetAtsB105BillEndoSubmit retAts =  new RetAtsB105BillEndoSubmit(query.reqHeader);
        RetAtsB105BillEndoSubmitDetail detail = new RetAtsB105BillEndoSubmitDetail();

        retAts.writeStart();
        XmlTextWriter xmlWriter  = new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTOPRBED";
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBEDX");
        Date d = new Date();
        String date = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        xmlWriter.writeElementString("EPTTIM",time);
        //分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.endoBnkArea);
        if("308584001024".equals(query.detail.endoBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.endoBnkArea.length()?bankarea:query.detail.endoBnkArea);

        xmlWriter.writeElementString("APPEAC",query.detail.actNum);
        if("1".equals(query.detail.transFlag)) {
            xmlWriter.writeElementString("NOTTRF", "EM00");
        }else{
            xmlWriter.writeElementString("NOTTRF", "EM01");
        }
        xmlWriter.writeElementString("OPCEAC",query.detail.receAct);
        xmlWriter.writeElementString("OPCNAM",query.detail.receName);
        xmlWriter.writeElementString("OPCBRD",query.detail.receBnkCode);
        xmlWriter.writeElementString("CLTREM","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRECDX");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        xmlWriter.writeElementString("BILNBR",query.detail.billNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
     * 贴现申请
     * */
    public RetAtsB106BillDiscSubmit D_B106_BillResp_Submit(ReqB106BillDiscSubmit query) throws Exception {
        RetAtsB106BillDiscSubmit retAts = new RetAtsB106BillDiscSubmit(query.reqHeader);
        RetAtsB106BillDiscSubmitDetail detail = new RetAtsB106BillDiscSubmitDetail();

        retAts.writeStart();
        XmlTextWriter xmlWriter = new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTOPRBDS";
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS", "");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBDSX");
        Date d = new Date();
        String date = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        xmlWriter.writeElementString("EPTTIM", time);
        //分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.discBnkArea);
        if("308584001024".equals(query.detail.discBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK", bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.discBnkArea.length()?bankarea:query.detail.discBnkArea);

        xmlWriter.writeElementString("APPEAC", query.detail.actNum);
        xmlWriter.writeElementString("BCHNBR", "");
        String discType;
        if ("1".equals(query.detail.discType)) {
            discType = "RM00";
        } else {
            discType = "RM01";
        }
        xmlWriter.writeElementString("DSCMOD", discType);
        xmlWriter.writeElementString("DISINR", query.detail.discRate);
        //币种
        xmlWriter.writeElementString("FCTCCY", "10");
        if ("1".equals(query.detail.transFlag)) {
            xmlWriter.writeElementString("NOTTRF", "EM00");
        } else {
            xmlWriter.writeElementString("NOTTRF", "EM01");
        }
        if ("1".equals(query.detail.payType)) {
            xmlWriter.writeElementString("DVPTAG", "SM00");
        } else {
            xmlWriter.writeElementString("DVPTAG", "SM01");
        }
        if ("RM01".equals(discType)) {
            xmlWriter.writeElementString("OPNDAT", query.detail.repOpenDate.replaceAll("-", ""));
            xmlWriter.writeElementString("ENDDAT", query.detail.repDueDate.replaceAll("-", ""));
            xmlWriter.writeElementString("BCKINR", query.detail.repRate);
            xmlWriter.writeElementString("BCKCCY", "10");
        } else {
            xmlWriter.writeElementString("OPNDAT", "");
            xmlWriter.writeElementString("ENDDAT", "");
            xmlWriter.writeElementString("BCKINR", "");
            xmlWriter.writeElementString("BCKCCY", "");
        }
        xmlWriter.writeElementString("CNTNBR", "");
        xmlWriter.writeElementString("INVNBR", "");
        if ("2".equals(query.detail.payType) && !"1".equals(query.detail.receSameBnk)) {
            //ecds检测贴现涉及3家机构会报错，入账账号可以传贴现行号，银行转钱的时候根据入账账号可以分析出开户行
            xmlWriter.writeElementString("ACCBRD", query.detail.receBnkCode);
            xmlWriter.writeElementString("ACCEAC", query.detail.actedAct);
        }else {
            xmlWriter.writeElementString("ACCBRD", "");
            xmlWriter.writeElementString("ACCEAC", "");
        }
        xmlWriter.writeElementString("OPCNAM",query.detail.receName);
        xmlWriter.writeElementString("OPCBRD",query.detail.receBnkCode);
        xmlWriter.writeElementString("OPCEAC",query.detail.receAct);

        if("1".equals(query.detail.payInterestType)){
            xmlWriter.writeElementString("INRPCT", "0");
        }else if("2".equals(query.detail.payInterestType)){
            xmlWriter.writeElementString("INRPCT", "100");
        }else{
            xmlWriter.writeElementString("INRPCT",""+query.detail.payInterestRate );
        }
        //协商加天数
        String adddatStr = query.detail.adjustDiscDay;
        if(StringHelper.hasAnyChar(adddatStr)){
            int adddat = Integer.valueOf(adddatStr);
            xmlWriter.writeElementString("ADDDAT", Math.abs(adddat)+"");
            xmlWriter.writeElementString("PLSFLG ",adddat<0?"N":"Y");
        }else{
            if("RM00".equals(discType)) {
                throw new Exception("买断式贴现的时候,协商加天数字段不能为空");
            }else{
                xmlWriter.writeElementString("ADDDAT", "");
                xmlWriter.writeElementString("PLSFLG ", "");
            }
        }
        xmlWriter.writeElementString("CLTREM",query.detail.memo);

        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRECDX");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        xmlWriter.writeElementString("BILNBR",query.detail.billNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRBDSZ/ERRTXT");
                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRBDSZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRBDSZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
     * 质押申请
     * */
    public RetAtsB107BillPledSubmit D_B107_BillResp_Submit(ReqB107BillPledSubmit query) throws Exception {
        RetAtsB107BillPledSubmit retAts =  new RetAtsB107BillPledSubmit(query.reqHeader);
        RetAtsB107BillPledSubmitDetail detail = new RetAtsB107BillPledSubmitDetail();

        retAts.writeStart();
        XmlTextWriter xmlWriter  = new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTOPRBIP";
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBIPX");
        Date d = new Date();
        String date = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        xmlWriter.writeElementString("EPTTIM",time);
        xmlWriter.writeElementString("BCHNBR","");
        //分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.pledBnkArea);
        if("308584001024".equals(query.detail.pledBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.pledBnkArea.length()?bankarea:query.detail.pledBnkArea);

        xmlWriter.writeElementString("APPEAC",query.detail.actNum);

        xmlWriter.writeElementString("CLTREM",query.detail.memo);
        xmlWriter.writeElementString("BIPEAC",query.detail.receAct);
        xmlWriter.writeElementString("BIPNAM",query.detail.receName);
        xmlWriter.writeElementString("BIPBRD",query.detail.receBnkCode);
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRECDX");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        xmlWriter.writeElementString("BILNBR",query.detail.billNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
    * 提示付款
    * */
    public RetAtsB108BillPaySubmit D_B108_BillResp_Submit(ReqB108BillPaySubmit query) throws Exception {
        RetAtsB108BillPaySubmit retAts =  new RetAtsB108BillPaySubmit(query.reqHeader);
        RetAtsB108BillPaySubmitDetail detail = new RetAtsB108BillPaySubmitDetail();

        retAts.writeStart();
        XmlTextWriter xmlWriter  = new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTOPRBPR";
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBPRX");
        Date d = new Date();
        String date = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        xmlWriter.writeElementString("EPTTIM",time);
        //分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.actArea);
        if("308584001024".equals(query.detail.actBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.actArea.length()?bankarea:query.detail.actArea);

        xmlWriter.writeElementString("APPEAC",query.detail.actNum);
//        xmlWriter.writeElementString("BCHNBR","");
        xmlWriter.writeElementString("CLTREM",query.detail.memo);
        if("2".equals(query.detail.proPayType)) {
            xmlWriter.writeElementString("BPRTYP", "21");
        }else{
            xmlWriter.writeElementString("BPRTYP", "20");
        }
        if("1".equals(query.detail.payType)) {
            xmlWriter.writeElementString("DVPFLG", "SM00");
        }else{
            xmlWriter.writeElementString("DVPFLG", "SM01");
        }
        xmlWriter.writeElementString("RTNTXT",query.detail.overDueRea);

        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRECDX");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        xmlWriter.writeElementString("BILNBR",query.detail.billNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
     * 保证申请 不支持
     * */
    public RetAtsB109BillGuarSubmit D_B109_BillResp_Submit(ReqB109BillGuarSubmit query) throws Exception {
        return null;
    }

    @Override
    /**
    *追索通知
     */
    public RetAtsB110BillRecoSubmit D_B110_BillResp_Submit(ReqB110BillRecoSubmit query) throws Exception {
        RetAtsB110BillRecoSubmit retAts =  new RetAtsB110BillRecoSubmit(query.reqHeader);
        RetAtsB110BillRecoSubmitDetail detail = new RetAtsB110BillRecoSubmitDetail();

        retAts.writeStart();
        XmlTextWriter xmlWriter  = new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTOPRBTR";
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBTRX");
        Date d = new Date();
        String date = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        xmlWriter.writeElementString("EPTTIM",time);
        //分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.actArea);
        if("308584001024".equals(query.detail.recoBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.actArea.length()?bankarea:query.detail.actArea);

        xmlWriter.writeElementString("APPEAC",query.detail.actNum);
        String recourseType = query.detail.recourseType;
        if("1".equals(recourseType)){
            xmlWriter.writeElementString("BTRTYP","RT00");
        }else{
            xmlWriter.writeElementString("BTRTYP","RT01");
        }
        if("2".equals(recourseType)) {
            if("1".equals(query.detail.recourseCode)) {
                xmlWriter.writeElementString("BTRCOD", "RC00");
            }else{
                xmlWriter.writeElementString("BTRCOD", "RC01");
            }
        }else{
            xmlWriter.writeElementString("BTRCOD","");
        }
//        追 索 通 知 备 注
        xmlWriter.writeElementString("CLTREM",query.detail.memo);
        //币种
        xmlWriter.writeElementString("BTRCCY","10");
        xmlWriter.writeElementString("BTRAMT",query.detail.billAmt);
        xmlWriter.writeElementString("BTREAC",query.detail.receAct);
        xmlWriter.writeElementString("BTRNAM",query.detail.receName);
        xmlWriter.writeElementString("BTRBRD",query.detail.receBnkCode);
        //被追索人组织机构代码
        xmlWriter.writeElementString("BTRORG",query.detail.receCode);

        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRECDX");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        xmlWriter.writeElementString("BILNBR",query.detail.billNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
     * 同意清偿
     * */
    public RetAtsB111BillAgreeSubmit D_B111_BillResp_Submit(ReqB111BillAgreeSubmit query) throws Exception {
        RetAtsB111BillAgreeSubmit retAts =  new RetAtsB111BillAgreeSubmit(query.reqHeader);
        RetAtsB111BillAgreeSubmitDetail detail = new RetAtsB111BillAgreeSubmitDetail();

        retAts.writeStart();
        XmlTextWriter xmlWriter  = new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTOPRBTP";
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBTPX");
        Date d = new Date();
        String date = Format.Date8Format.format(d);
        String time = Format.Time6Format.format(d);
        String begindateTest = getBankConfig().getValueAsString("queryDate");
        if(StringHelper.hasAnyChar(begindateTest)){
            date = begindateTest;
        }
        xmlWriter.writeElementString("EPTDAT",date);
        xmlWriter.writeElementString("EPTTIM",time);
        //分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.agreeBnkArea);
        if("308584001024".equals(query.detail.agreeBnkCode)){
            bankarea = "1";
        }

//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.agreeBnkArea.length()?bankarea:query.detail.agreeBnkArea);

        xmlWriter.writeElementString("APPEAC",query.detail.actNum);
        xmlWriter.writeElementString("CLTREM",query.detail.memo);

        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTOPRBTPX1");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);
        xmlWriter.writeElementString("BILNBR",query.detail.billNo);
        xmlWriter.writeElementString("BTPCCY","10");
        xmlWriter.writeElementString("BTPAMT",query.detail.agreeAmt);
        xmlWriter.writeElementString("CLTKEY",query.detail.busiNo);
        xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    /**
    * 交易撤回
    * */
    public RetAtsB112BillCancSubmit D_B112_BillResp_Submit(ReqB112BillCancSubmit query) throws Exception {
        RetAtsB112BillCancSubmit retAts =  new RetAtsB112BillCancSubmit(query.reqHeader);
        RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();

        retAts.writeStart();
        XmlTextWriter xmlWriter  = new XmlTextWriter();
        CMB01Header header = new CMB01Header();
        header.FUNNAM = "SDKNTCTSCNL";
        query.readRecord();
        writeCommonHeader(xmlWriter,header);

        xmlWriter.writeStartElement("NTOPRMODX");
        String busMod = getBankConfig().getValueAsString("busmodELE");
        if(StringHelper.isNullOrEmpty(busMod)){
            throw new Exception("请检查config.xml文件中的busmodELE是否配置");
        }
        xmlWriter.writeElementString("BUSMOD",busMod);
        xmlWriter.writeElementString("MODALS","");
        xmlWriter.writeEndElement();
        Date d = new Date();
        String date = Format.Date8Format.format(d);
//        saveAppleDate(query.detail.reqSeqID,date);
        xmlWriter.writeStartElement("NTCTSCNLX");
        //分行号
        String bankarea = getBankConfig().getMapValueByKey("bankArea",query.detail.actArea);
        if("308584001024".equals(query.detail.cancBnkCode)){
            bankarea = "1";
        }
//        xmlWriter.writeElementString("APPBBK",bankarea);
        //如果ats传过来的直联区域，则直接发送给银行
        xmlWriter.writeElementString("APPBBK",4==query.detail.actArea.length()?bankarea:query.detail.actArea);

        xmlWriter.writeElementString("APPEAC",query.detail.actNum);

        xmlWriter.writeEndElement();

        xmlWriter.writeStartElement("NTCTSCNLX1");
        xmlWriter.writeElementString("SQRNBR","0000000001");
        xmlWriter.writeElementString("YURREF",query.detail.reqSeqID);

//        23=同意清偿撤销
//        22=追索通知撤销
//        21=逾期提示付款撤销
//        19=质押解除撤销
//        18=质押撤销
//        20=提示付款撤销
//        17=保证申请撤销
//        11=贴现撤销
//        10=背书转让撤销
        String cancelType;
        switch (Integer.valueOf(query.detail.cancelType)){
            //撤回类型 1-承兑申请撤回 2-提示收票申请撤回 3-背书转让申请撤回 4-贴现申请撤回 5-质押申请撤回 6-（逾期）提示付款撤回
//    7-出票保证申请撤回 8-承兑保证申请撤回  9-背书保证申请撤回 10-追索通知撤回 11-同意清偿撤回 12-解质押申请撤回
            case 1:cancelType="02";break;
            case 2:cancelType="03";break;
            case 3:cancelType="10";break;
            case 4:cancelType="11";break;
            case 5:cancelType="18";break;
            case 6:
                if("2".equals(query.detail.dueOverFlag)) {
                    cancelType = "21";
                }else{
                    cancelType = "20";
                }break;//（逾期）提示付款撤回
            case 7:
            case 8:
            case 9:throw new Exception("不支持保证撤销操作");
            case 10:cancelType="22";break;
            case 11:cancelType="23";break;
            case 12:cancelType="19";break;
            default: {
                throw new Exception("位置的撤销操作:" + query.detail.cancelType);
            }
        }
        xmlWriter.writeElementString("BILBUS",cancelType);

        //票据业务代码
        xmlWriter.writeElementString("CLTKEY", query.detail.busiNo);
        xmlWriter.writeEndDocument();
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
        if(tmpStrRet[0].length() == 0)
        {
            StringBuilder getBankRetInfo = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
            if (tmpBkReader != null)
            {
                String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                if("0".equals(bankRetCode)) {
                    detail.respCode ="0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                    String errCodd =tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                    if("SUC0000".equals(errCodd)) {
                        detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                    }else{
                        detail.transState = errCodd;
                    }
                }else{
                    detail.respCode = "0";
                    detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                    detail.transState = bankRetCode;
                }
            }
            else
            {
                detail.respCode = "-1";
                detail.respInfo = tmpBkReader.toString();
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[0].toString();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB113ProtocolSubmit D_B113_ProtocolResp_Submit(ReqB113ProtocolSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB119BillRelePledSubmit D_B119_BillResp_Submit(ReqB119BillRelePledSubmit query) throws Exception {
        RetAtsB119BillRelePledSubmit retAts =  new RetAtsB119BillRelePledSubmit(query.reqHeader);
        retAts.writeStart();
        RetAtsB119BillRelePledSubmitDetail detail = new RetAtsB119BillRelePledSubmitDetail();
        query.readRecord();
       try {
           XmlTextWriter xmlWriter = new XmlTextWriter();
           CMB01Header header = new CMB01Header();
           header.FUNNAM = "SDKNTOPRBIR";
           writeCommonHeader(xmlWriter, header);

           xmlWriter.writeStartElement("NTOPRMODX");
           String busMod = getBankConfig().getValueAsString("busmodELE");
           if (StringHelper.isNullOrEmpty(busMod)) {
               throw new Exception("请检查config.xml文件中的busmodELE是否配置");
           }
           xmlWriter.writeElementString("BUSMOD", busMod);
           xmlWriter.writeElementString("MODALS", "");
           xmlWriter.writeEndElement();

           xmlWriter.writeStartElement("NTOPRBIRX");
           Date d = new Date();
           String date = Format.Date8Format.format(d);
           String time = Format.Time6Format.format(d);
           String begindateTest = getBankConfig().getValueAsString("queryDate");
           if (StringHelper.hasAnyChar(begindateTest)) {
               date = begindateTest;
           }
           xmlWriter.writeElementString("EPTDAT", date);
           xmlWriter.writeElementString("EPTTIM", time);
           //分行号
           String bankarea = getBankConfig().getMapValueByKey("bankArea", query.detail.pledgeeBnkArea);
           if ("308584001024".equals(query.detail.pledgeeBnkArea)) {
               bankarea = "1";
           }
//        xmlWriter.writeElementString("APPBBK",bankarea);
           //如果ats传过来的直联区域，则直接发送给银行
           xmlWriter.writeElementString("APPBBK", 4 == query.detail.pledgeeBnkArea.length() ? bankarea : query.detail.pledgeeBnkArea);

//           APPEAC 申请人账号 C(35) 否
           xmlWriter.writeElementString("APPEAC",query.detail.actNum);
//           CLTREM 质权人备注 Z(800) 可
           xmlWriter.writeElementString("CLTREM",query.detail.memo);
//           BCHNBR 批次号 C(10) 可
           xmlWriter.writeElementString("BCHNBR","");
           xmlWriter.writeEndElement();

           xmlWriter.writeStartElement("NTOPRECDX");
           xmlWriter.writeElementString("SQRNBR", "0000000001");
           xmlWriter.writeElementString("YURREF", query.detail.reqSeqID);
           xmlWriter.writeElementString("BILNBR", query.detail.billNo);
           xmlWriter.writeEndDocument();
//        saveAppleDate(query.detail.reqSeqID,date);
           String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

           if (tmpStrRet[0].length() == 0) {
               StringBuilder getBankRetInfo = new StringBuilder();
               //解析银行返回
               XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
               if (tmpBkReader != null) {
                   String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                   if ("0".equals(bankRetCode)) {
                       detail.respCode = "0";
                       detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRTXT");

                       String errCodd = tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/ERRCOD");
                       if ("SUC0000".equals(errCodd)) {
                           detail.transState = errCodd + "+" + tmpBkReader.getFirstText("/CMBSDKPGK/NTOPRRTNZ/REQSTS");
                       } else {
                           detail.transState = errCodd;
                       }
                   } else {
                       detail.respCode = "0";
                       detail.respInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                       detail.transState = bankRetCode;
                   }
               } else {
                   detail.respCode = "-1";
                   detail.respInfo = tmpBkReader.toString();
               }
           } else {
               detail.respCode = "-1";
               detail.respInfo = tmpStrRet[0].toString();
           }
       }catch (Exception ex){
           ex.getStackTrace();
           detail.respCode ="-1";
           detail.respInfo =ex.getMessage();
       }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }
}
